package cn.meye11.crm.mapper;

import cn.meye11.crm.common.DateUtils;
import cn.meye11.crm.common.RegexUtils;
import cn.meye11.crm.common.StringUtils;
import cn.meye11.crm.domain.entity.CostEntity;
import cn.meye11.crm.domain.query.CostQuery;
import org.apache.ibatis.jdbc.SQL;

/**
 * (Cost)表数据库访问层 SQL组合类
 * 
 * @author makejava
 * @since 2024-01-03 14:41:34
 */
 
public class CostProvider {

    public String list(CostQuery query) {

        SQL sql = new SQL();
        sql.SELECT("*");
        sql.FROM(CostEntity.Table + " cos ");
        sql.WHERE(wheres(query));
        sql.ORDER_BY(query.getSort() + " " + query.getDirection());
        return sql.toString();
    }

    public String count(CostQuery query) {

        SQL sql = new SQL();
        sql.SELECT("count (id)");
        sql.FROM(CostEntity.Table + " cos ");
        sql.WHERE(wheres(query));
        return sql.toString();
    }

    private String wheres(CostQuery query) {
        StringBuilder where = new StringBuilder();
        if (query != null) {
            // 不为空时，必须为逗号分割的数字才能查询
            String id = StringUtils.commaDelimitedTrimEmptyString(query.getId());
            if (StringUtils.isNotBlank(id) && RegexUtils.check(RegexUtils.Type.ONLY_NUMBER_OR_EMPTY, id)) {
                where.append("cos.id in (" + id + ")").append(" and ");
            }
            if (StringUtils.isNotBlank(query.getName())) {
                where.append("cos.name = '" + query.getName() + "'").append(" and ");
            }
            if (StringUtils.isNotBlank(query.getNameLike())) {
                where.append("cos.name like '%" + query.getNameLike() + "%'").append(" and ");
            }
            String nameIn = StringUtils.toSqlInString(query.getNameIn());
            if (StringUtils.isNotBlank(nameIn)) {
                where.append("cos.name in (" + nameIn + ")").append(" and ");
            }
            String nameNotIn = StringUtils.toSqlInString(query.getNameNotIn());
            if (StringUtils.isNotBlank(nameNotIn)) {
                where.append("cos.name not in (" + nameNotIn + ")").append(" and ");
            }
            if (query.getCostPrice() != null) {
                where.append("cos.cost_price = " + query.getCostPrice()).append(" and ");
            }
            if (StringUtils.isNotBlank(query.getOperators())) {
                where.append("cos.operators = '" + query.getOperators() + "'").append(" and ");
            }
            if (StringUtils.isNotBlank(query.getOperatorsLike())) {
                where.append("cos.operators like '%" + query.getOperatorsLike() + "%'").append(" and ");
            }
            String operatorsIn = StringUtils.toSqlInString(query.getOperatorsIn());
            if (StringUtils.isNotBlank(operatorsIn)) {
                where.append("cos.operators in (" + operatorsIn + ")").append(" and ");
            }
            String operatorsNotIn = StringUtils.toSqlInString(query.getOperatorsNotIn());
            if (StringUtils.isNotBlank(operatorsNotIn)) {
                where.append("cos.operators not in (" + operatorsNotIn + ")").append(" and ");
            }
            if (StringUtils.isNotBlank(query.getLabel())) {
                where.append("cos.label = '" + query.getLabel() + "'").append(" and ");
            }
            if (StringUtils.isNotBlank(query.getLabelLike())) {
                where.append("cos.label like '%" + query.getLabelLike() + "%'").append(" and ");
            }
            String labelIn = StringUtils.toSqlInString(query.getLabelIn());
            if (StringUtils.isNotBlank(labelIn)) {
                where.append("cos.label in (" + labelIn + ")").append(" and ");
            }
            String labelNotIn = StringUtils.toSqlInString(query.getLabelNotIn());
            if (StringUtils.isNotBlank(labelNotIn)) {
                where.append("cos.label not in (" + labelNotIn + ")").append(" and ");
            }
            if (query.getStart() != null) {
                where.append("cos.time >= '" + DateUtils.localDateTimeToStr(query.getStart()) + "'").append(" and ");
            }
            if (query.getEnd() != null) {
                where.append("cos.time <= '" + DateUtils.localDateTimeToStr(query.getEnd()) + "'").append(" and ");
            }
        }
        where.append("cos.del='0'");
        return where.toString();
    }
}
